// async await的中间件开发示例

module.exports = async (ctx, next) => {
  ctx.post = ctx.request.post
  ctx.get = ctx.request.query
  ctx.params = ctx.request.params
  ctx.json = (code = 200, msg = 'success', data = []) => {
    if (typeof code !== 'number') {
      if (typeof code === 'string') {
        msg = code
        code = 200
      } else {
        data = code
        code = 200
        msg = 'success'
      }
    }
    ctx.body = {
      code: code,
      msg: msg,
      data: data,
    }
  }
  ctx.error = (code, err = '') => {
    if (typeof code !== 'number') {
      err = code
      code = 500
    }
    ctx.body = {
      code: code,
      msg: err,
      data: [],
    }
  }
  await next()
}